﻿using Dicom;
using NAudio.Midi;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;


namespace MedVis_MidiController
{

    public partial class frmMain : Form
    {
        public delegate void ProgressBarChangerDelegate(float progressPercent);
        public ProgressBarChangerDelegate progressBarChangerDelegate;

        public delegate void ImageChangerDelegate(Image img);
        public ImageChangerDelegate imageChangerDelegate;

        public delegate void UpdatePictureBoxLocationDelegate();
        public UpdatePictureBoxLocationDelegate updatePictureBoxLocationDelegate;

        public delegate void UpdatePreDefinedWindowsDelegate(int newIndexOffset);
        public UpdatePreDefinedWindowsDelegate updatePreDefinedWindowsDelegate;

        public delegate void UpdatePreDefinedWindowsAbsoluteDelegate(int newIndex);
        public UpdatePreDefinedWindowsAbsoluteDelegate updatePreDefinedWindowsAbsoluteDelegate;

        private DicomImageDispatcher dicomImgDis = new DicomImageDispatcher();

        private Point lastZoomedSize = default(Point);
        private float windowWidthPercent = 1.0f;
        private float windowCenterPercent = 1.0f;
        private Point startPos = new Point();
        private frSetting settingsWindow = null;
        private float oldZoomFactor = 1.0f;
        private float gDPI = 0;
        private Point mouseDown;
        private Point mouseStart;

        private PointF imgOff { get; set; }
        
        //Midi Klassen
        private MidiIn midi = null;
        public string MidiDeviceName = "";

        public frmMain()
        {
            InitializeComponent();
            UsbNotification.RegisterUsbDeviceNotification(this.Handle);

            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.SendToBack();

            this.MouseWheel += panel_MouseWheel;
            panel1.MouseWheel += panel_MouseWheel;
            pictureBox1.MouseWheel += panel_MouseWheel;

            if (MidiIn.NumberOfDevices > 0)
            {
                tlstlblDeviceName.Text = MidiIn.DeviceInfo(0).ProductName;
                MidiDeviceName = MidiIn.DeviceInfo(0).ProductName;
                midi = new MidiIn(0);

                midi.MessageReceived += midi_MessageReceived;
                midi.ErrorReceived += midi_ErrorReceived;
                
                midi.Start();
            }

            settingsWindow = new frSetting(this);

            imageChangerDelegate = new ImageChangerDelegate(ImageChanger);
            progressBarChangerDelegate = new ProgressBarChangerDelegate(ProgressBarChanger);
            updatePictureBoxLocationDelegate = new UpdatePictureBoxLocationDelegate(UpdatePictureBoxLocation);
            updatePreDefinedWindowsDelegate = new UpdatePreDefinedWindowsDelegate(UpdatePreDefinedWindows);
            updatePreDefinedWindowsAbsoluteDelegate = new UpdatePreDefinedWindowsAbsoluteDelegate(UpdatePreDefinedAbsoluteWindows);

            Graphics g = this.CreateGraphics();
            gDPI = g.DpiX;
            mouseDown = new Point(0, 0);
            startPos = new Point(pictureBox1.Width/2, pictureBox1.Height/2);
            imgOff = new Point(0, 0);

           comboBox_preDefinedWindows.ComboBox.DataSource = Enum.GetValues(typeof(preDefinedWindows.Enum));
        }

        private void UpdatePreDefinedAbsoluteWindows(int newIndexOffset)
        {
            newIndexOffset %= comboBox_preDefinedWindows.Items.Count;

            if (newIndexOffset < 0)
                newIndexOffset = comboBox_preDefinedWindows.Items.Count + newIndexOffset;
            
            comboBox_preDefinedWindows.SelectedIndex = newIndexOffset;
        }

        private void UpdatePreDefinedWindows(int newIndexOffset)
        {
            newIndexOffset %= comboBox_preDefinedWindows.Items.Count;

            Console.WriteLine(newIndexOffset);

            if (newIndexOffset < 0)
            {
                newIndexOffset = comboBox_preDefinedWindows.Items.Count + newIndexOffset;
            }


            comboBox_preDefinedWindows.SelectedIndex = (comboBox_preDefinedWindows.SelectedIndex + newIndexOffset) % comboBox_preDefinedWindows.Items.Count;
        }

        private void UpdatePictureBoxLocation()
        {
            pictureBox1.Left = (int)imgOff.X;
            pictureBox1.Top = (int)imgOff.Y;
        }

        private void ProgressBarChanger(float progressPercent)
        {
            if (progressPercent > 0.99)
                progressPercent = 1.0f;
            progressBar.Value = (int)(progressBar.Maximum * progressPercent);
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == UsbNotification.WmDevicechange)
            {
                switch ((int)m.WParam)
                {
                    case UsbNotification.DbtDeviceremovecomplete:
                        //TODO: keine Idee ob das gut ist wenn etwas rausgenommen wird.
                        if (midi != null && MidiIn.NumberOfDevices == 0)
                        {
                            tlstlblDeviceName.Text = "kein Device angeschlossen";
                            MidiDeviceName = "";
                            
                            midi.Dispose();
                        }
                        break;
                    case UsbNotification.DbtDevicearrival:
                        if (MidiIn.NumberOfDevices > 0)
                        {
                            tlstlblDeviceName.Text = MidiIn.DeviceInfo(0).ProductName;
                            MidiDeviceName = MidiIn.DeviceInfo(0).ProductName;
                            midi = new MidiIn(0);

                            midi.MessageReceived += midi_MessageReceived;
                            midi.ErrorReceived += midi_ErrorReceived;

                            midi.Start();
                        }
                        break;
                }
            }
        }

        public void ImageChanger(Image img)
        {
            if (lastZoomedSize == default(Point))
            {
                pictureBox1.Width = img.Width;
                pictureBox1.Height = img.Height;
                imgOff = new PointF(panel1.Width / 2.0f - pictureBox1.Width / 2.0f,
                                   panel1.Height / 2.0f - pictureBox1.Height / 2.0f);
                lastZoomedSize = new Point(img.Width, img.Height);

                oldZoomFactor = dicomImgDis.zoom;


            }

            Label_windowCenter.Text = "WindowCenter: " + ((int)(windowCenterPercent * 2000.0 - 1000.0));
            Label_windowWidth.Text = "WindowWidth: " + ((int)(windowWidthPercent * 2000.0));


            if (oldZoomFactor != dicomImgDis.zoom)
            {
                lastZoomedSize = new Point((int)(img.Width * dicomImgDis.zoom), (int)(img.Height * dicomImgDis.zoom));

                float newXPercent = 0.5f;
                float newYPercent = 0.5f;

                float oneYPercent = 100.0f / (panel1.Height - lastZoomedSize.Y);
                float oneXPercent = 100.0f / (panel1.Width - lastZoomedSize.X);

                settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                      delegate(MidiControllerSettings mCS)
                      {
                          return mCS.func == MidiControllerFunctions.Verschieben_Oben_Unten || mCS.func == MidiControllerFunctions.Verschieben_Links_Rechts;
                      }
                  ).ForEach(
                      delegate(MidiControllerSettings mCS)
                      {
                          if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                          {
                              if (mCS.func == MidiControllerFunctions.Verschieben_Oben_Unten)
                                  newYPercent += (mCS.lastKnownValue * mCS.functionParameter - 63.0f * mCS.functionParameter) / 127.0f / 100.0f;
                              else if (mCS.func == MidiControllerFunctions.Verschieben_Links_Rechts)
                                  newXPercent += (mCS.lastKnownValue * mCS.functionParameter - 63.0f * mCS.functionParameter) / 127.0f / 100.0f;
                          }
                          else
                          {
                              if (mCS.func == MidiControllerFunctions.Verschieben_Oben_Unten)
                                  newYPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneYPercent;
                              else if (mCS.func == MidiControllerFunctions.Verschieben_Links_Rechts)
                                newXPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneXPercent;
                          }
                      }
                  );
                imgOff = new PointF((panel1.Width - lastZoomedSize.X) * newXPercent, (panel1.Height - lastZoomedSize.Y) * newYPercent);
                pictureBox1.Width = lastZoomedSize.X;
                pictureBox1.Height = lastZoomedSize.Y;
            }
            pictureBox1.Left = (int)imgOff.X;
            pictureBox1.Top = (int)imgOff.Y;

            pictureBox1.Image = img;
        }

        private void midi_ErrorReceived(object sender, MidiInMessageEventArgs e)
        {
            throw new NotImplementedException();
        }

        private void midi_MessageReceived(object sender, MidiInMessageEventArgs e)
        {

            if (e.MidiEvent != null && e.MidiEvent.CommandCode == MidiCommandCode.AutoSensing)
                return;

            if (!(e.MidiEvent is ControlChangeEvent))
                return;

            ControlChangeEvent changeEvent = (ControlChangeEvent)e.MidiEvent;
            if (settingsWindow.Visible)
                settingsWindow.Invoke(settingsWindow.lastActiveDelegate,
                                   new Object[] { (byte)changeEvent.Controller, changeEvent.ControllerValue });
            //else
                //settingsWindow.lastActiveController((byte)changeEvent.Controller, changeEvent.ControllerValue, dicomImgDis);


            MidiControllerSettings activeController = settingsWindow.midiDeviceSettings.midiControllerSettings.Find(
               delegate(MidiControllerSettings mCS)
               {
                   return mCS.controlNumber == (byte)changeEvent.Controller;
               }
           );
            
            if (activeController == null)
                return;

            activeController.lastKnownValue = (byte)changeEvent.ControllerValue;

            int biggest = 0;

            switch( activeController.func )
            {
                case MidiControllerFunctions.Keine:
                    break;
                case MidiControllerFunctions.Durch_Vordefinierte_Windows_Wandern:
                    if( activeController.type == MidiControllerType.Knopf )
                    {
                        if (activeController.lastKnownValue == 0)
                            return;

                        int offsetAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            offsetAddition = (int)(comboBox_preDefinedWindows.Items.Count * activeController.functionParameter / 100.0f);
                        else
                            offsetAddition = activeController.functionParameter;

                        this.Invoke(this.updatePreDefinedWindowsDelegate,
                                    new Object[] { offsetAddition });


                    }
                    else
                    {
                        float newPreIndexPercent = 0.5f;
                        float onePreIndexPercent = 100.0f / (comboBox_preDefinedWindows.Items.Count);

                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.Durch_Vordefinierte_Windows_Wandern;
                           }
                        ).ForEach(
                            delegate(MidiControllerSettings mCS)
                            {
                                if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                                {
                                    newPreIndexPercent += ((mCS.lastKnownValue - 63.0f)) / 127.0f * mCS.functionParameter / 100.0f;
                                }
                                else if( mCS.functionParameterType == MidiControllerFunctionParameterType.Absolut)
                                {
                                    newPreIndexPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * onePreIndexPercent;
                                }
                            }
                        );


                        this.Invoke(this.updatePreDefinedWindowsAbsoluteDelegate,
                                    new Object[] { (int)((comboBox_preDefinedWindows.Items.Count * newPreIndexPercent) % comboBox_preDefinedWindows.Items.Count) });


                        
                    }


                    break;
                case MidiControllerFunctions.Verschieben_Links_Rechts:
                    if (activeController.type == MidiControllerType.Knopf)
                    {
                        if (activeController.lastKnownValue == 0)
                            return;

                        float offsetAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            offsetAddition = lastZoomedSize.X * activeController.functionParameter / 100.0f;
                        else
                            offsetAddition = activeController.functionParameter;

                        imgOff = new PointF(imgOff.X + offsetAddition, imgOff.Y);
                    }
                    else
                    {
                        float newXPercent = 0.5f;
                        float oneXPercent = 100.0f / (panel1.Width - lastZoomedSize.X);

                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.Verschieben_Links_Rechts;
                           }
                       ).ForEach(
                           delegate(MidiControllerSettings mCS)
                           {
                               if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                               {
                                   newXPercent += ((mCS.lastKnownValue - 63.0f)) / 127.0f * mCS.functionParameter / 100.0f;

                               }
                               else
                               {
                                   newXPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneXPercent;
                               }
                           }
                       );

                        imgOff = new PointF( (panel1.Width-lastZoomedSize.X) * newXPercent ,imgOff.Y);
                    }
                    
                    this.Invoke(this.updatePictureBoxLocationDelegate);
                        
                    break;
                case MidiControllerFunctions.Verschieben_Oben_Unten:
                    if( activeController.type == MidiControllerType.Knopf )
                    {
                        if( activeController.lastKnownValue == 0 )
                            return;

                        float offsetAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            offsetAddition = lastZoomedSize.Y * activeController.functionParameter / 100.0f;
                        else
                            offsetAddition = activeController.functionParameter;

                        imgOff = new PointF(imgOff.X, imgOff.Y + offsetAddition);
                    }
                    else
                    {
                        float newYPercent = 0.5f;
                        float oneYPercent = 100.0f / (panel1.Height - lastZoomedSize.Y);

                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.Verschieben_Oben_Unten;
                           }
                       ).ForEach(
                           delegate(MidiControllerSettings mCS)
                           {
                               if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                               {
                                   newYPercent += (mCS.lastKnownValue * mCS.functionParameter - 63.0f * mCS.functionParameter) / 127.0f / 100.0f;

                               }
                               else
                               {
                                   newYPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneYPercent;
                               }
                           }
                       );
                        //-(lastZoomedSize.Y - panel1.Height)
                        imgOff = new PointF(imgOff.X, (panel1.Height - lastZoomedSize.Y) * newYPercent);
                    }


                    this.Invoke(this.updatePictureBoxLocationDelegate);
                        
                    break;
                case MidiControllerFunctions.Zoom:

                    if (activeController.type == MidiControllerType.Knopf)
                    {
                        if (activeController.lastKnownValue == 0)
                            return;

                        float zoomAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            zoomAddition = activeController.functionParameter / 100.0f;
                        else
                            zoomAddition = activeController.functionParameter / 100.0f;
                        dicomImgDis.zoom += zoomAddition;
                    }
                    else
                    {
                        float oneZoomPercent = 100.0f / (lastZoomedSize.X);
                        float newZoomPercent = 0.0f;
                        biggest = 0;

                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.Zoom;
                           }
                       ).ForEach(
                           delegate(MidiControllerSettings mCS)
                           {
                               if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                               {
                                   newZoomPercent += (mCS.lastKnownValue * mCS.functionParameter - 63.0f * mCS.functionParameter) / 127.0f / 100.0f;
                                   //if (mCS.functionParameter > biggest)
                                   biggest += mCS.functionParameter;
                               }
                               else
                               {
                                   newZoomPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneZoomPercent;
                                   biggest += mCS.functionParameter;
                               }
                           }
                       );
                      dicomImgDis.zoom = newZoomPercent + biggest / 2.0f / 100.0f;
                    }

                    
                    break;
                case MidiControllerFunctions.Scrolling:

                    if (activeController.type == MidiControllerType.Knopf)
                    {
                        if (activeController.lastKnownValue == 0)
                            return;

                        int indexAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            indexAddition = activeController.functionParameter;
                        else
                            indexAddition = activeController.functionParameter;


                        dicomImgDis.Index += indexAddition;
                    }
                    else
                    {
                        float oneIndexPercent = 100.0f / (dicomImgDis.MaxIndex + 1);
                        float newIndexPercent = 0.5f;


                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.Scrolling;
                           }
                       ).ForEach(
                           delegate(MidiControllerSettings mCS)
                           {
                               if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                                   newIndexPercent += (mCS.lastKnownValue * mCS.functionParameter - 63 * mCS.functionParameter) / 127.0f / 100.0f;
                               else
                               {
                                   newIndexPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneIndexPercent;
                               }
                           }
                       );
                        dicomImgDis.Index = (int)((dicomImgDis.MaxIndex + 1) * newIndexPercent);
                    }
                    break;

                case MidiControllerFunctions.WindowCenter:
                    float oneCenterPercent = 100.0f / (2000);

                    if (activeController.type == MidiControllerType.Knopf)
                    {
                        if (activeController.lastKnownValue == 0)
                            return;


                        float widthAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            widthAddition = activeController.functionParameter/100.0f * oneCenterPercent;
                        else
                            widthAddition = activeController.functionParameter / 100.0f * oneCenterPercent;


                        windowCenterPercent += widthAddition;
                        dicomImgDis.changeWindow(windowCenterPercent * 2000.0 - 1000.0, windowWidthPercent * 2000.0);
                        comboBox_preDefinedWindows.SelectedItem = preDefinedWindows.Enum.Keins;
                    }
                    else
                    {
                        float newCenterPercent = 0.5f;

                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.WindowCenter;
                           }
                       ).ForEach(
                           delegate(MidiControllerSettings mCS)
                           {
                               if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                                   newCenterPercent += (mCS.lastKnownValue * mCS.functionParameter - 63 * mCS.functionParameter) / 127.0f / 100.0f;
                               else
                               {
                                   newCenterPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneCenterPercent;
                               }
                           }
                       );
                        windowCenterPercent = newCenterPercent;
                        //Console.WriteLine("windowCenter: " +( windowCenterPercent * 2000.0 - 1000.0 )+"---windowWidth: "+( windowWidthPercent * 2000.0));
                        dicomImgDis.changeWindow(windowCenterPercent * 2000.0 - 1000.0, windowWidthPercent * 2000.0);
                        comboBox_preDefinedWindows.SelectedItem = preDefinedWindows.Enum.Keins;
                    }
                    break;

                case MidiControllerFunctions.WindowWidth:
                    float oneWidthPercent = 100.0f / (2000);
                    float newWidthPercent = 0.5f;
                    
                    if (activeController.type == MidiControllerType.Knopf)
                    {
                        if (activeController.lastKnownValue == 0)
                            return;


                        float widthAddition = 0;
                        if (activeController.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                            widthAddition = activeController.functionParameter / 100.0f * oneWidthPercent;
                        else
                            widthAddition = activeController.functionParameter / 100.0f * oneWidthPercent;


                        windowWidthPercent += widthAddition; 
                        dicomImgDis.changeWindow(windowCenterPercent * 2000.0 - 1000.0, windowWidthPercent * 2000.0);
                        comboBox_preDefinedWindows.SelectedItem = preDefinedWindows.Enum.Keins;
                    }
                    else
                    {

                        settingsWindow.midiDeviceSettings.midiControllerSettings.FindAll(
                           delegate(MidiControllerSettings mCS)
                           {
                               return mCS.func == MidiControllerFunctions.WindowWidth;
                           }
                       ).ForEach(
                           delegate(MidiControllerSettings mCS)
                           {
                               if (mCS.functionParameterType == MidiControllerFunctionParameterType.Prozent)
                                   newWidthPercent += (mCS.lastKnownValue * mCS.functionParameter - 63 * mCS.functionParameter) / 128.0f / 100.0f;
                               else
                               {
                                   newWidthPercent += ((mCS.lastKnownValue - 63.5f)) / 127.0f * mCS.functionParameter / 100.0f * oneWidthPercent;
                               }
                           }
                       );
                        windowWidthPercent = newWidthPercent;
                        //Console.WriteLine("windowCenter: " +( windowCenterPercent * 2000.0 - 1000.0 )+"---windowWidth: "+( windowWidthPercent * 2000.0));
                        dicomImgDis.changeWindow(windowCenterPercent * 2000.0 - 1000.0, windowWidthPercent * 2000.0);
                        comboBox_preDefinedWindows.SelectedItem = preDefinedWindows.Enum.Keins;
                    }
                    break;
                default:
                    break;
            }


        }

        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            this.panel_MouseWheel(sender, e);
        }

        private void panel_MouseWheel(object sender, MouseEventArgs e)
        {
            if (tlZoom.Checked)
            {
                float oldzoom = dicomImgDis.zoom;

                if (e.Delta > 0)
                {
                    dicomImgDis.zoom += 0.1F;
                }

                else if (e.Delta < 0)
                {
                    dicomImgDis.zoom = Math.Max(dicomImgDis.zoom - 0.1F, 0.01F);
                }
            }
            else
            {
                if (e.Delta > 0)
                {
                    dicomImgDis.Index++;
                }
                else
                {
                    dicomImgDis.Index--;
                }
            }
        }

        private void ladenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.DirectoryInfo ParentDirectory = new System.IO.DirectoryInfo(folderBrowserDialog1.SelectedPath);
                List<string> fileList = new List<string>();
                

                foreach (System.IO.FileInfo f in ParentDirectory.GetFiles())
                {
                    if (f.Extension != ".dcm")
                        continue;

                    try
                    {
                        DicomFile.Open(f.FullName);
                        fileList.Add(f.FullName);
                    }
                    catch(Exception exc)
                    {}


                }
                if (dicomImgDis != null)
                    dicomImgDis.Dispose();
                dicomImgDis = new DicomImageDispatcher(pictureBox1, fileList.ToArray(), this);
                comboBox_preDefinedWindows.SelectedItem = preDefinedWindows.Enum.Keins;
                
                progressBar.Visible = true;
                dicomImgDis.Index = 0;
            }
        }

        private void beendenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
        }
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            dicomImgDis.Dispose();

            settingsWindow.Close();
            //Zum beenden des MidiInDevice
            if (midi != null)
            {
                midi.Stop();
                midi.Dispose();
            }
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            settingsWindow.Show();
        }

        private void comboBox_preDefinedWindows_SelectedIndexChanged(object sender, EventArgs e)
        {
            var preWindow = preDefinedWindows.getWindowByEnum((preDefinedWindows.Enum)comboBox_preDefinedWindows.ComboBox.SelectedItem);
            
            if (preWindow == null)
                return;

            dicomImgDis.changeWindow(preWindow.windowCenter, preWindow.windowWidth);
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                panel1.Focus();
                startPos = e.Location;
                mouseDown = e.Location;
                mouseStart = new Point((int)imgOff.X, (int)imgOff.Y);
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                int deltaX = e.Location.X - mouseDown.X; // the distance the mouse has been moved since mouse was pressed
                int deltaY = e.Location.Y - mouseDown.Y;

                if (tlMovePic.Checked)
                {
                    imgOff = new Point(mouseStart.X + deltaX, mouseStart.Y + deltaY);

                    pictureBox1.Left = (int)imgOff.X;
                    pictureBox1.Top = (int)imgOff.Y;
                }
                else
                {
                    int deltaX_ = e.Location.X - startPos.X; // the distance the mouse has been moved since mouse was pressed
                    int deltaY_ = e.Location.Y - startPos.Y;

                    windowCenterPercent += deltaX_ * 0.005f;
                    windowWidthPercent += deltaY_ * 0.005f;
                    dicomImgDis.changeWindow(windowCenterPercent * 2000.0 - 1000.0, windowWidthPercent * 2000.0);
                    comboBox_preDefinedWindows.SelectedItem = preDefinedWindows.Enum.Keins;
                    startPos = e.Location;
                }
            }
        }

        private void pictureBox1_MouseDown_1(object sender, MouseEventArgs e)
        {
            int xTrans = e.X + pictureBox1.Location.X;
            int yTrans = e.Y + pictureBox1.Location.Y;
            MouseEventArgs eTrans = new MouseEventArgs(e.Button, e.Clicks, xTrans, yTrans, e.Delta);
            panel1_MouseDown(sender, eTrans);
        }

        private void pictureBox1_MouseMove_1(object sender, MouseEventArgs e)
        {
            int xTrans = e.X + pictureBox1.Location.X;
            int yTrans = e.Y + pictureBox1.Location.Y;
            MouseEventArgs eTrans = new MouseEventArgs(e.Button, e.Clicks, xTrans, yTrans, e.Delta);
            panel1_MouseMove(sender, eTrans);
        }

        private void toolchange(object sender, EventArgs e)
        {
            if (sender == tlFenstern || sender == tlMovePic)
            {
                tlFenstern.Checked = sender == tlFenstern;
                tlMovePic.Checked = sender == tlMovePic;
            }

            if (sender == tlScrollPic || sender == tlZoom)
            {
                tlScrollPic.Checked = sender == tlScrollPic;
                tlZoom.Checked = sender == tlZoom;
            }
        }

        private void comboBox_preDefinedWindows_DropDownClosed(object sender, EventArgs e)
        {
            panel1.Focus();
        }
    }

}
